<template>
    <div class="bg">
        <div class="login-container">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-position="left" label-width="0px"
                class="login-form">
                <h3 class="title">用户画像生成系统</h3>
                <!-- <el-form-item prop="username">
          <el-input type="text" v-model="loginForm.username" autocomplete="off" placeholder="请输入手机号"></el-input>
        </el-form-item> -->
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" autocomplete="off"
                        placeholder="请输入登录码"></el-input>
                </el-form-item>
                <!-- <el-form-item prop="code" class="yzm">

          <el-input placeholder="请输入验证码" v-model="loginForm.code" style="width: 60%;margin-right:10px;"></el-input> <img style="width: 100px;height: 40px;" :src="yzm">
        </el-form-item> -->

                <el-form-item style="width: 100%;">
                    <el-button type="primary" style="width: 100%;" @click="handleLogin">登录</el-button>
                </el-form-item>
                <div class="tip" @click="sjzhuce()">
                    管理员登录

                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import service from '@/request';

export default {
    data() {
        return {
            loginForm: {
                username: '',
                password: '',
                code: ''
            },
            yzm: "",
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入验证码', trigger: 'blur' }

                ],
                // role: [
                //   { required: true, message: '请选择角色', trigger: 'change' }
                // ]
            }
        };
    },
    mounted() {
        this.getchatimge()
    },
    methods: {
        async getchatimge() {
            const res = await service.get("/auth/captcha");
            console.log(res)
            this.yzm = res.data.img
            this.loginForm.uuid = res.data.uuid
        },
        sjzhuce() {
            this.$router.push({
                path: "/"
            })
        },
        handleLogin() {
            this.$refs.loginForm.validate(async (valid) => {
                if (valid) {
                    this.loginForm.username = this.loginForm.password
                    const res = await service.post("/auth/login", this.loginForm);
                    console.log(res)
                    if (res.code == 200) {
                        this.$message.success("登录成功");


                        this.$router.push({
                            path: "/ceping",
                            query:{
                                id:this.loginForm.username
                            }
                        })
                        window.localStorage.setItem("token", res.data.token)
                  

                    }
                } else {
                    console.log('表单校验失败');
                    return false;
                }
            });
        }
    }
}
</script>

<style scoped>
.tip {
    color: #2a60c9;
    font-size: 14px;
    cursor: pointer;
    text-align: right;
}

.login-container {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    background-size: 100% 100%;
    border-radius: 4px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.title {
    text-align: center;
    margin-bottom: 20px;
    font-size: 24px;
    color: #333;
}

.login-form {
    width: 100%;
}

.el-input {
    width: 100%;
}

.bg {
    width: 100%;
    height: 100%;
    background: url("../assets/login.png") no-repeat;
    background-size: 100% 100%;

}
</style>
<style>
.yzm .el-form-item__content {
    display: flex;
    align-items: center;
}
</style>